﻿package Cain.MkMc 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	/**
	 * Loading畫面
	 * @author Cain
	 */
	public class LoadingMc extends AllMk {
		private var _root:Sprite; //root
		private var mkAlpha:Number; //透明度
		private var mkColor:Number; //顏色
		private var in_s:Number; //淡入時間
		private var loading_class:Class; //元件庫動畫類別
		private var loading_mc:DisplayObject; //loading動畫
		private var str:String;
		/**
		 * @param	_mc
		 * @param	_alpha
		 * @param	_color
		 * @param	_s
		 * @param	_str 等待顯示文字
		 */
		public function LoadingMc(_mc:DisplayObject,_str:String="",_alpha:Number = .5,_color:uint = 0,_s:Number=.5) 
		{
			_root = Sprite(_mc.root);
			mkAlpha = _alpha;
			mkColor = _color;
			in_s = _s;
			str = _str;
			super(_root, mkAlpha, mkColor, in_s);
			addEventListener(Event.ADDED_TO_STAGE, addthis);
		}
		/**
		 * 設定loading動畫
		 * @param	_loadingMovieclip 元件庫裡的影片類別名
		 */
		public function setup(_loadingMovieclip:String=null):void
		{
			if (_loadingMovieclip!=null) 
			{
				loading_class = getDefinitionByName(_loadingMovieclip) as Class;
			}
			
		}
		
		private function addthis(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addthis);
			addEventListener(Event.REMOVED_FROM_STAGE, renovethis);
		}
		/**
		 * 進場
		 */
		public function loading():void
		{
			if (loading_class==null) 
			{
				if (str=="") 
				{
					loading_mc = new LoadingWd();
				}else 
				{
					loading_mc = new LoadingWd(str);
				}
				
			}else 
			{
				loading_mc = new loading_class() as DisplayObject;
				loading_mc.x = _root.stage.stageWidth >> 1;
				loading_mc.y = _root.stage.stageHeight >> 1;
			}
			addChild(loading_mc);
			init();
			_root.stage.addEventListener(Event.RESIZE, onRESIZE);
		}
		
		private function onRESIZE(e:Event):void 
		{
			loading_mc.x = _root.stage.stageWidth >> 1;
			loading_mc.y = _root.stage.stageHeight >> 1;
		}
		/**
		 * 退場
		 * @param	_s 退場時間
		 */
		public function unloading(_s:Number = .5):void 
		{
			try 
			{
				_root.stage.removeEventListener(Event.RESIZE, onRESIZE);
				removeChild(loading_mc);
				loading_mc = null;
				unload(_s);
			}catch (err:Error)
			{
				
			}
			
		}
		private function renovethis(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, renovethis);
		}
	}
	
}